<?php
include 'common.php';
session_start();

// initialize our variables
$selected_categories = null;
$categories_bundle = null;

// if the user has selected some categories on selection.php 
if (isset($_SESSION['selected_categories'])) {
    $selected_categories = unserialize($_SESSION['selected_categories']);
    unset($_SESSION['selected_categories']);
    unset($_SESSION['imageShield']);
}

// if the user has requested different categories, clear the imageshield and selected categories
if (isset($_REQUEST['reset']) && $_REQUEST['reset'] == 'true') {
    unset($_SESSION['selected_categories']);
    unset($_SESSION['imageShield']);
}

// load the imageShield data 
if (isset($_SESSION['imageShield'])) {
    $imageShield = unserialize($_SESSION['imageShield']);
    $categories_bundle = $imageShield['categories_bundle'];
    $selected_categories = $imageShield['selected_categories'];
}

// generate imageShield data
$imageShield = generate_imageShield(9, $selected_categories, $categories_bundle);

// store the imageShield data
$_SESSION['imageShield'] = serialize($imageShield);

// parse the imageShield data
extract($imageShield);

// if this page is requested by an AJAX script, display the imageShield data as XML
if (is_ajax_request()) {
    header('Content-type: application/xml');
    echo format_imageshield_response($imageShield_URI, $imageShield_id, $category_name_1, $category_name_2);
}

/*
 * returns an array of data neccessary for enrolling a user
 */
function generate_imageShield($count, $selected_categories=null, $categories_bundle=null) {
    global $vs;

    $categories = $selected_categories;
    if($categories_bundle != null)
        $categories = $categories_bundle;

    // generate an imageShield bundle
    $es = $vs->get_service('enrollment');
    $categories_bundle = $es->get_random_bundle($count, $categories);
   
    // generate an imageShield from the imageShield bundle 
    $iss = $vs->get_service('imageshield');
    $imageShield = $iss->new_imageshield(array('bundle'=>$categories_bundle));

    // parse the imageShield response
    return array('categories_bundle' => $categories_bundle,
                 'selected_categories' => $selected_categories,
                 'category_name_1' => $es->get_category($categories_bundle[0])->name,
                 'category_name_2' => $es->get_category($categories_bundle[1])->name,
                 'imageShield_URI' => $imageShield->image,
                 'imageShield_id' => $imageShield->id );
}

/**
 * Most JS AJAX libraries, including jQuery, set an HTTP_X_REQUESTED_WITH
 * header, so that browsers and servers can properly handle the request
 */
function is_ajax_request(){
    return ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' );
}

/**
 * format the reponse data from an imageshield request
 */
function format_imageshield_response($uri, $id, $category_name_1, $category_name_2) {
    $xmlstr =  '<?xml version="1.0" encoding="iso-8859-1"?>';
    $xmlstr .= "<imageshield>";
    $xmlstr .= "    <uri>$uri</uri>";
    $xmlstr .= "    <id>$id</id>";
    $xmlstr .= "    <category_name_1>".$category_name_1."</category_name_1>";
    $xmlstr .= "    <category_name_2>".$category_name_2."</category_name_2>";
    $xmlstr .= "</imageshield>";
    return $xmlstr;
}
